﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Media;
using System.Threading;
using System.Collections;
using System.IO;
using System.Xml;

/* Authors: Mike Poddo, Matt Gillette
 * Missile Command
 */

namespace MissileCommand
{
    public partial class MissileCommand : Form
    {
        private List<DefensiveMissileShots> DefensiveShots;
        private static List<OffensiveMissileShots> OffensiveShots;
        private Graphics g;
        private System.Windows.Forms.Timer OffensiveShooter;
        private System.Windows.Forms.Timer MonitorGame;
        private SoundPlayer sp;
        private int leftMaxMissiles;
        private int centerMaxMissiles;
        private int rightMaxMissiles;
        private int endpt;
        private static int missileHits = 0;
        private Cities cities;
        private static int[] OffShotList = new int[6]  {0,0,0,0,0,0};
        private Boolean MouseListenersPause;
        private Boolean debug = false;
        private Boolean developer = true;
        private Boolean paused = false;
        private Boolean progressiveSpeed = false; //assume easy mode
        private static int OldScore;
        private static int PlayerScore = 0;
        private static String message = "";
        private String userName;
        private Form EnterInformation = new EnterInformation();

        public MissileCommand()
        {
            InitializeComponent();
            Form ei = new EnterInformation();
            ei.StartPosition = FormStartPosition.CenterScreen;
            ei.ShowDialog();
            StartGame();
        }

        private void StartGame()
        {
            if (MessageBox.Show("Welcome to Missile Command " + Properties.Settings.Default.userName + "!\n\nWould you like to play with increasing missile speed?", "Start Game", MessageBoxButtons.YesNo) == DialogResult.Yes)
                progressiveSpeed = true;

            userName = Properties.Settings.Default.userName;
            MonitorGame = new System.Windows.Forms.Timer();
            MonitorGame.Interval = 100;
            MonitorGame.Start();
            MonitorGame.Tick += GameUpdater;
            OffensiveShooter = new System.Windows.Forms.Timer();
            OffensiveShooter.Interval = 2000;
            OffensiveShooter.Start();
            DefensiveShots = new List<DefensiveMissileShots>();
            OffensiveShots = new List<OffensiveMissileShots>();
            cities = new Cities(this.CreateGraphics());
            cities.drawCities();
            MouseListenersPause = false;

            if (developer)
            {
                sp = new SoundPlayer("sounds/godlike.wav");
                sp.Play();
                leftMaxMissiles = 999;
                centerMaxMissiles = 999;
                rightMaxMissiles = 999;
            }
            else
            {
                sp = new SoundPlayer("sounds/play.wav");
                sp.Play();
                leftMaxMissiles = 10;
                centerMaxMissiles = 10;
                rightMaxMissiles = 10;
            }

            MouseUp += new MouseEventHandler(FireMissile_Click);
            OffensiveShooter.Tick += new EventHandler(FireOffensiveMissile);
            pauseGame();
        }

        private static void ClearScores()
        {
            OldScore = 0;
            PlayerScore = 0;
            for (int x = 0; x < OffShotList.Length; x++)
                OffShotList[x] = 0;
        }

        public static int addMissileHit
        {
            set
            {
                missileHits++;
            }
        }

        public static void ModifyShotList(int spot)
        {
            OffShotList[spot] = 0;
        }

        private void GameUpdater(object sender, System.EventArgs e)
        {
            int RemainingCities = cities.CitiesAccessor;
            if (RemainingCities == 0) //all yo citiez r ded mofo!!!
            {
                pauseGame();
                MonitorGame.Stop();
                String insert = "";
                if (PlayerScore < 1000)
                    insert = "like a girl";
                else if (PlayerScore > 1000 && PlayerScore < 5000)
                    insert = "like you barely cared";
                else if (PlayerScore > 5000 && PlayerScore < 10000)
                    insert = "superbly";
                else if (PlayerScore > 10000 && PlayerScore < 20000)
                    insert = "heroicly";
                else if (PlayerScore > 30000)
                    insert = "like Tony Temple";

                message = "All of your cities have been destroyed by the aliens!\n\n" +
                          "You fought " + insert + " and achieved a score of: " + PlayerScore + "!\n\n";

                //Properties.Settings.Default.HighScores = "";
                HighScore hs = new HighScore(userName, PlayerScore);
                Properties.Settings.Default.HighScores += "\n" + hs.ToString();
                Properties.Settings.Default.Save();
                if (MessageBox.Show(message + Properties.Settings.Default.HighScores, "High Score List", MessageBoxButtons.OK) == DialogResult.OK)
                    Close();

            }
            else
            {
                g = CreateGraphics();
                tslCities.Text = "Cities Remaining: " + RemainingCities;
                g.FillRectangle(new SolidBrush(Color.Black), 50, 50, 100, 50);
                Font f = new Font("Veranda", 25);
                g.DrawString("" + PlayerScore, f, new SolidBrush(Color.Blue), 50, 50);
            }
        }


        public static void ModifyPlayerScore(int add)
        {
            OldScore = PlayerScore;
            PlayerScore += add;
        }

        internal static List<OffensiveMissileShots> getTheList()
        {
            return OffensiveShots;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            cities.drawCities();
            for(int x = 0; x< DefensiveShots.Count; x++) //delete missile shots that have been completed
                if (DefensiveShots[x].isActive() == false)
                   DefensiveShots.Remove(DefensiveShots[x]);
        }

        private int RandomNumber(int size)
        {
            Random random = new Random();
            return random.Next(size); //size of possible starting points
        }

        #region Missile Firing (Offensive and Defensive)
        private void FireOffensiveMissile(object sender, System.EventArgs e)
        {
            if (debug)
            {
                string s = "";
                for (int x = 0; x < OffShotList.Length; x++)
                    s += OffShotList[x].ToString() + " | Missile hits: " + missileHits;
                tslStatusText.Text = s;
            }

            endpt = RandomNumber(5);

            while (OffShotList[endpt] != 0)
                endpt = RandomNumber(6);

            OffShotList[endpt] = 1;

            int pointsPerHit = 123;
            if (progressiveSpeed)
            {
                if (missileHits > 10)
                {
                    OffensiveShooter.Interval = 1500;
                    pointsPerHit = 171;
                }
                else if (missileHits > 20)
                {
                    OffensiveShooter.Interval = 1250;
                    pointsPerHit = 266;
                }
                else if (missileHits > 40)
                {
                    OffensiveShooter.Interval = 800;
                    pointsPerHit = 389;
                }
                else if (missileHits > 80)
                {
                    OffensiveShooter.Interval = 500;
                    pointsPerHit = 502;
                }
                OffensiveMissileShots oms = new OffensiveMissileShots(this.CreateGraphics(), ref cities, endpt, pointsPerHit);
                OffensiveShots.Add(oms);
            }
            else
            {
                OffensiveMissileShots oms = new OffensiveMissileShots(this.CreateGraphics(), ref cities, endpt, pointsPerHit);
                OffensiveShots.Add(oms);
            }
            
        }

        private void FireMissile_Click(object sender, MouseEventArgs e)
        {
            if (!MouseListenersPause)
            {
            sp = new SoundPlayer("sounds/missile_launch.wav");
            if (e.Button == MouseButtons.Left)
                if (leftMaxMissiles > 0)
                {
                    sp.Play();
                    DefensiveShots.Add(new DefensiveMissileShots(60, 300, e.X, e.Y, this.CreateGraphics()));
                    PlayerScore -= 42;
                    leftMaxMissiles--;
                }
            if (e.Button == MouseButtons.Middle)
                if (centerMaxMissiles > 0)
                {
                    sp.Play();
                    DefensiveShots.Add(new DefensiveMissileShots(193, 300, e.X, e.Y, this.CreateGraphics()));
                    PlayerScore -= 42;
                    centerMaxMissiles--;
                }
            if (e.Button == MouseButtons.Right)
                if (rightMaxMissiles > 0)
                {
                    sp.Play();
                    DefensiveShots.Add(new DefensiveMissileShots(345, 300, e.X, e.Y, this.CreateGraphics()));
                    PlayerScore -= 42;
                    rightMaxMissiles--;
                }
            }
        }
        #endregion

        #region Pause Game
        private void pauseGame()
        {
            if(paused)
            {
                //game already paused, restart it and flip the paused
                tslStatusText.Text = "";
                for (int x = 0; x < OffensiveShots.Count; x++)
                {
                    OffensiveShots[x].UnPauseMe();
                }
                for (int x = 0; x < DefensiveShots.Count; x++)
                {
                    DefensiveShots[x].UnPauseMe();
                }
                OffensiveShooter.Start();
                paused = !paused;
                MouseListenersPause = false;
            }
            else
            {
                //game is not paused, pause it and flip the paused
                tslStatusText.Text = "***Game Paused***";
                for(int x = 0; x < OffensiveShots.Count; x++ )
                {
                    OffensiveShots[x].PauseMe();
                }
                for (int x = 0; x < DefensiveShots.Count; x++)
                {
                    DefensiveShots[x].PauseMe();
                }
                OffensiveShooter.Stop();
                paused = !paused;
                MouseListenersPause = true;
            }            
        }

        private void pauseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pauseGame();
        }
        #endregion

        #region Closing Game
        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {

            Close();
        }

        private void MissileCommand_FormClosing(object sender, FormClosingEventArgs e)
        {
            
            Close();
        }
        #endregion

        private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StartGame();
        }

        private void instantLoseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            cities.CitiesAccessor = 0;
        }

        private void aboutTheGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pauseGame();
            MessageBox.Show("Missile Command is a 1980 arcade game by Atari Inc. that was also licensed to Sega for European release. \n"+
                            "The plot of Missile Command is simple: the player's six cities are being attacked by an endless hail of ballistic missiles, \n"+
                            "some of them even splitting like multiple independently targetable reentry vehicles (MIRVs), "+
                            "and in later levels smart bombs which can evade a less than perfectly targeted missile. As a regional commander of three anti-missile batteries, "+
                            "the player must defend six cities in their zone from being destroyed.\n\n"+
                            "This version created by Mike Poddo and Matt Gillette.");
        }

        private void helpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pauseGame();
            MessageBox.Show("Shoot the missiles, defend your cities.");
        }


    }//END class
}
